

window.Bublifuk = new (function() {

  var openmenu = null;
  var current_button = null;

  this.add_button_click = function(sender, options)  {

    if (openmenu) {
      openmenu.destroy();
    }

    current_button = sender;
    YAHOO.util.Dom.addClass(sender, 'SubmitButton-pushed');

    //var items = [];

    //console.log(options['action']);

    var add_context_menu = openmenu =
      new YAHOO.widget.ContextMenu(
          "fieldcontextmenu",
          {
              zIndex:999,
              iframe:false,
              itemdata: options['itemdata'],
                /*[
                 {
                    text: "Field color",
                    submenu: {
                        id: "fieldcolors",
                        itemdata: [
                            { text: "Light Green", onclick: { fn: setFieldColor, obj: "#99cc66" }, checked: true },
                            { text: "Medium Green", onclick: { fn: setFieldColor, obj: "#669933" } },
                            { text: "Dark Green", onclick: { fn: setFieldColor, obj: "#336600" } }
                        ]
                    }
                },
                { text: "Delete all", onclick: { fn: deleteEwes } },
                { text: "New Ewe", onclick: { fn: createNewEwe } }
              ],*/
              lazyload: true
          }
      );


    add_context_menu.render(document.body);
    var pos = YAHOO.util.Dom.getXY(sender);
    console.log(pos);
    add_context_menu.moveTo(pos[0], pos[1]+24);
    add_context_menu.show();
    add_context_menu.subscribe('beforeHide', function() {
      if (current_button) {
        YAHOO.util.Dom.removeClass(current_button, 'SubmitButton-pushed');
      }
    });

    add_context_menu.subscribe('click', function(e, info) {
      add_thing_field = document.getElementById(options['action'])
      console.log('h', add_context_menu);
      console.log('ello', add_context_menu.activeItem);
      add_thing_field.value = info[1].value;
      add_thing_field.form.submit();
      //console.log(add_context_menu.activeItem.value);
      //sender.
    });

  }


})();
